//
//  LBMStatusPictureView.swift
//  微博
//
//  Created by 刘保满 on 16/5/25.
//  Copyright © 2016年 itheima. All rights reserved.
// 微博配图视图

import UIKit

//图片间距
private let pictureMargin: CGFloat = 3
//最大宽度
private let maxWidth = ScreenWidth - 2 * StatusCellMargin

//单张图片的宽度
private let itemWidth = (maxWidth - 2 * pictureMargin) / 3

//注册标记
private let pictureCellId = "pictureCellId"

class LBMStatusPictureView: UICollectionView {

   //数据源
    var pictures: [LBMPicture]? {
        
        didSet {
            
            testLabel.text = "\(pictures?.count ?? 0)"
            
            let pSize = calculatePictureViewSize()
            
            self.snp_updateConstraints { (make) in
                
                
                make.size.equalTo(pSize)
            }
            
            reloadData()
            
        }
        
    }
    
    
    
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = pictureMargin
        layout.minimumInteritemSpacing = pictureMargin
        super.init(frame: frame, collectionViewLayout: layout)
        
        self.dataSource = self
        
        backgroundColor = UIColor.whiteColor()
        
        //设置不能响应点击状态栏快速回到顶部 //这样tableview 中点击状态栏就可以快速回到顶部了,因为一个视图界面不允许两个可以响应的视图
        scrollsToTop = false
        
        //注册
        registerClass(LBMStatusPictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        setupUI()
        
        
        
        
        
    }
    
    //默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI 
    private func setupUI() {
        //添加测试label
        addSubview(testLabel)
        //设置约束
        testLabel.snp_makeConstraints { (make) in
            
            make.center.equalTo(self.snp_center)
            
            
        }
        
    }
    //懒加载测试label
    private lazy var testLabel: UILabel = UILabel(title: "", font: 30, textColor: UIColor.redColor())

    
    //确定配图的大小
    func calculatePictureViewSize() -> CGSize {
        
        //拿到配图的数量
        let imageCount = pictures?.count ?? 0
        
        //0张图
        if imageCount == 0 {
            
            return CGSizeZero
            
        }
        
        //获取布局对象
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        
        //1张图片
        if imageCount == 1 {
            
            
            let imageSize = CGSizeMake(100, 100)
            //设置itemsize和图片一样的
            layout.itemSize = imageSize
            
            return imageSize
            
        }
        
        //设置其他多张图片的大小
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        //4张
        if imageCount == 4 {
            
            let w = itemWidth * 2 + pictureMargin
            
            return CGSizeMake(w, w)
  
        }
        
        
        let row = CGFloat((imageCount - 1) / 3) + 1
        
        let maxHeight = row * itemWidth + (row - 1) * pictureMargin
        
        return CGSizeMake(maxWidth, maxHeight)
        
        
    }
    


}
//数据源
extension LBMStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return pictures?.count ?? 0
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! LBMStatusPictureCell
        
//        cell.backgroundColor = randomColor()
        
        //设置数据
        cell.picture = pictures![indexPath.item]
        
        return cell
        

        
    }
    

    
}


class LBMStatusPictureCell: UICollectionViewCell {
    
    //给cell 设置模型
    var picture: LBMPicture?  {
        
        didSet {
            let url = NSURL(string: picture?.thumbnail_pic ?? "")
            
            imageView.sd_setImageWithURL(url)
            
        }
        

    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI() {
        
        contentView.addSubview(imageView)
        //设置约束
        imageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView.snp_edges)
            
        }
        
        
        
    }
    
    //懒加载
    private lazy var imageView: UIImageView = {
        
        let im = UIImageView()
        //解决图片拉伸问题
        im.contentMode = .ScaleAspectFill
        im.clipsToBounds = true
        
        return im
        
        
    }()
    
    
}















